//
//  CoGeCIRenderer.m
//  CoGeOpenSource
//
//  Created by Tamas Nagy on 9/5/12.
//  Copyright (c) 2012 home. All rights reserved.
//

#import "CoGeCIRenderer.h"
#import <OpenGL/CGLMacro.h>
@implementation CoGeCIRenderer

@synthesize filter;
@synthesize compositionFilePath;
@synthesize filterName;
@synthesize coreImageContext;
@synthesize currentCIImage;

-(id)initCoGeCIRendererWithPixelFormat:(NSOpenGLPixelFormat *)format shareContext:(NSOpenGLContext *)shareContext {
        
    if (self = [super init]) {
    
        self.filter = nil;
        
        
        colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
        
        [self setCompositionFilePath:@""];
        
        [self setFilterName:@""];
        
        cgl_ctx = [[[CoGeRenderManager sharedManager] glContext] CGLContextObj];
        colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
        
        [self setCoreImageContext:[CIContext contextWithCGLContext:cgl_ctx pixelFormat:CGLGetPixelFormat(cgl_ctx) colorSpace:colorspace options:NULL]];

        
        return self;

    } else {
        
        NSLog(@"cannot init CoGeRenderer object!");

        return nil;
    }
    
    
    
}




-(BOOL)canUseMSAA {
    
    return NO;
}


-(void)setRenderSize:(NSSize)newSize {
    
	renderSize = newSize;
    
}

- (NSSize)textureSize
{
	return renderSize;
}


-(NSSize)renderSize {
	
	return renderSize;
}

- (GLuint)textureName
{
    if (!texture) {
        
        CGLLockContext(cgl_ctx);
        
        //  CGLSetCurrentContext(cgl_ctx);
        
        // texture / color attachment
        glGenTextures(1, &texture);
        // glEnable(GL_TEXTURE_RECTANGLE_EXT);
        glBindTexture(GL_TEXTURE_RECTANGLE_EXT, texture);
        glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, renderSize.width, renderSize.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
        glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 0);
        
        CGLUnlockContext(cgl_ctx);
        
    }
    
    return texture;
}


-(void)renderAtTime:(NSTimeInterval)time withArguments:(NSDictionary *)arguments {
    
    CGLLockContext(cgl_ctx);
    CGRect currentRect = CGRectMake(0, 0, renderSize.width, renderSize.height);
    
    [[self coreImageContext] drawImage:self.currentCIImage inRect:currentRect fromRect:currentRect];
    [[self coreImageContext] clearCaches];

    CGLUnlockContext(cgl_ctx);

}

#pragma mark Compability with CoGeRenderer
-(id)composition {
    
    return self;
}

//for compability, we will mark outputkeys to always be nil
//so the "outputImage" key won't mark our renderer as a CoGeRenderer with ported out outputImage value
-(NSArray *)outputKeys {
    
    return nil;
}


-(void)cleanUpOnRenderingThread {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    CGLLockContext(cgl_ctx);
    
    if(texture)
    {
        glDeleteTextures(1, &texture);
        texture = 0;
    }
    
    CGLUnlockContext(cgl_ctx);
    
    currentCIImage = nil;
    
    if (filter) {
        [filter release];
        filter = nil;
    }
    
    
    [filterName release];
	
    
    
    if (compositionFilePath) {
        
        [compositionFilePath release];
        compositionFilePath = nil;
    }
    
    
    CGColorSpaceRelease(colorSpace);

}

-(void)dealloc {
    
    
    [super dealloc];
}

@end
